﻿define(function(require, exports, modules) {
	var $ = require('jquery');
	require("jqueryMore");
	require('jquery.cookie');
	require('Base64');
	var Common = {
		storage: window.localStorage,
		tabsScroll: null,
		isOpened: false, //我的子菜单是否打开
		isOpened_mid: false, //中间的子菜单是否打开
		isOpened_info: false, //资讯的子菜单是否打开
		showChild: function(type) {
			var isLogined = Common.storage.getItem('isLogined');
			if(isLogined == "0" || isLogined == null) {
				//window.location.href = "login.html";
				$("#login_btn").show();
			} else {
				$("#login_btn").hide();
			}
			if(type == "1") {
				if(Common.isOpened == false) {

					$("#child_menu").show();
					$("#mid_menu").hide();
					$("#info_menu").hide();
					Common.isOpened = true;
					Common.isOpened_mid = false;
					Common.isOpened_info = false;
					if(localStorage.getItem('isLogined') == 1) {
						$('#child_menu #register_btn').hide();
						$('#account_btn').css('border-radius', '0.4em 0.4em 0 0');
						$('#account_btn').css('border-top', '1px solid #b5b5b5');
					} else {
						$('#child_menu #register_btn').show();
						$('#account_btn').css('border-radius', '0');
						$('#account_btn').css('border-top', 'none');
					}
				} else {
					$("#child_menu").hide();
					$("#mid_menu").hide();
					$("#info_menu").hide();
					Common.isOpened = false;
					Common.isOpened_mid = false;
					Common.isOpened_info = false;
				}
			} else if(type == "2") {

				if(Common.isOpened_mid == false) {
					if((localStorage.getItem("loginType") == "employee") && (localStorage.getItem("isLogined") == 1)) {
						Common.goPage('2');
						return false;
					} else {
						$("#mid_menu").show();
						$("#child_menu").hide();
						$("#info_menu").hide();
						Common.isOpened_mid = true;
						Common.isOpened = false;
						Common.isOpened_info = false;
					}
				} else {
					$("#child_menu").hide();
					$("#mid_menu").hide();
					$("#info_menu").hide();
					Common.isOpened = false;
					Common.isOpened_mid = false;
					Common.isOpened_info = false;
				}
			} else if(type == "3") {
				if(Common.isOpened_info == false) {
					$("#info_menu").show();
					$("#mid_menu").hide();
					$("#child_menu").hide();
					Common.isOpened_mid = false;
					Common.isOpened = false;
					Common.isOpened_info = true;
				} else {
					$("#info_menu").hide();
					$("#mid_menu").hide();
					$("#child_menu").hide();
					Common.isOpened = false;
					Common.isOpened_mid = false;
					Common.isOpened_info = false;
				}
			}
		},
		judgeToken: function(userId, fn) {
			return;
			if(userId) {
				$.M.post({
					method: '/sys/getToken?brokerId=' + userId,
					params: {},
					success: function(data) {
						data = eval('(' + data + ')');
						if(data.objValue) {
							localStorage.setItem("userToken", data.objValue)
							fn && fn();
						}
					}
				}, "0");
			}

		},
		getBase64number: function(s) {
			if($.M.is_encoding == 1 && s != '' && s != null) {
				s = Base64.decode(s.substring(1, s.length - 1));
				return s;
			} else {
				return s;
			}
		},
		setBase64number: function(s) {
			if($.M.is_encoding == 1 && s != '' && s != null) {
				s = 'z' + Base64.encode(s) + 'a';
				return s;
			} else {
				return s;
			}
		},
		goPage: function(pageId) {
			var loginType = Common.storage.getItem("loginType");
			var pageId = pageId + "";
			switch(pageId) {
				case '1':
					window.location.href = "propertyList.html?companyId=" + Common.storage.getItem("cityId") + "&from=" + Common.storage.getItem("from") + "&oldCompanyId=" + Common.storage.getItem("oldCompanyId");
					break;
				case '2':
					window.location.href = "recommendPage.html?companyId=" + Common.storage.getItem("cityId") + "&from=" + Common.storage.getItem("from") + "&oldCompanyId=" + Common.storage.getItem("oldCompanyId");
					break;
				case '3':
					window.location.href = "registerPage.html?companyId=" + Common.storage.getItem("cityId") + "&from=" + Common.storage.getItem("from") + "&oldCompanyId=" + Common.storage.getItem("oldCompanyId");
					break;
				case '4':
					window.location.href = "login.html?companyId=" + Common.storage.getItem("cityId") + "&from=" + Common.storage.getItem("from") + "&oldCompanyId=" + Common.storage.getItem("oldCompanyId");
					break;
				case '5':
					if(undefined == loginType || loginType == "customer") {
						window.location.href = "myAccount.html?companyId=" + Common.storage.getItem("cityId") + "&from=" + Common.storage.getItem("from") + "&oldCompanyId=" + Common.storage.getItem("oldCompanyId");
					} else if(loginType == "employee") {
						window.location.href = "myAccount2.html?companyId=" + Common.storage.getItem("cityId") + "&from=" + Common.storage.getItem("from") + "&oldCompanyId=" + Common.storage.getItem("oldCompanyId");
					}
					break;
				case '6':
					window.location.href = $.M.downloadUrl;
					break;
				case '7':
					window.location.href = "bespeakPage.html?companyId=" + Common.storage.getItem("cityId") + "&from=" + Common.storage.getItem("from") + "&oldCompanyId=" + Common.storage.getItem("oldCompanyId");
					break;
				case '8':
					window.location.href = "newsList.html?companyId=" + Common.storage.getItem("cityId") + "&from=" + Common.storage.getItem("from") + "&oldCompanyId=" + Common.storage.getItem("oldCompanyId");
					break;
				case '9':
					window.location.href = "houseNewsList.html?companyId=" + Common.storage.getItem("cityId") + "&from=" + Common.storage.getItem("from") + "&oldCompanyId=" + Common.storage.getItem("oldCompanyId");
					break;
				default:
					break;
			}
		},
		getQueryString: function(name) { 
			var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");     
			var r = window.location.search.substr(1).match(reg);     
			if(r != null) return  unescape(r[2]);
			return null;
		},
		showMask: function(theme, msg) {
			$.mobile.showPageLoadingMsg(theme, msg);
			$(".mask_div").css("display", "block");
		},
		hideMask: function(theme, msg) {
			$.mobile.hidePageLoadingMsg(theme, msg);
			$(".mask_div").css("display", "none");
		},
		showMessges: function(messges) {
			if($("#error").length== 0) {
				$("body").append('<div id="error" class="error_div">' +
					'<span id="error_word" class="error"></span>' +
					'</div>')
			}

			$("#error_word").html(messges);
			$("#error").css("visibility", "visible");
			setTimeout('$("#error").css("visibility", "hidden")', 3000);
		},
		checkToken: function(token, fn) {
			Common.showMask("c", "正在加载...");
			$.M.post({
				method: "/rest/hengDaBaoRest/checkToken",
				params: {
					token: token
				},
				success: function(data) {
					data = eval('(' + data + ')');
					//alert(JSON.stringify(data));
					Common.hideMask("c", "正在加载...");
					if(data.objValue) {
						if(!data.objValue.employeeVO) {
							Common.storage.setItem("loginType", "customer");
							Common.storage.setItem("userId", data.objValue.id);
							Common.storage.setItem("isLogined", "1");
							Common.storage.setItem("token", token);
							Common.storage.setItem("phone", data.objValue.mphone);
							var fromFrag = Common.storage.getItem("from");
							if(fromFrag == "null") {
								fromFrag = eval(fromFrag);
							}
							if(fromFrag) {
								Common.storage.setItem("cityId", data.objValue.companyId);
								Common.storage.setItem("cityname", data.objValue.companyAlias);
							}
							if(data.objValue.dormantStatus == "1") { //该字段为1表示休眠状态

							} else {
								//设置需要检查休眠，行业，实名认证
								Common.storage.setItem("needCheck", "1");
							}
						} else {
							Common.storage.setItem("loginType", "employee");
							Common.storage.setItem("isLogined", "1");
							Common.storage.setItem("token", token);
							Common.storage.setItem("userId", data.objValue.employeeVO.brokerId);
							Common.storage.setItem("eId", data.objValue.employeeVO.employeeNo);
						}
					} else {
						//$('body').html("<p style='text-align:center;'>"+data.value+"</p>")
					}
				},
				error: function(XMLHttpRequest, textStatus, errorThrown) {

				}
			}, "0");
		},
		getAppVersion: function(fn) {
			return;
			$.M.post({
				method: '/sys/getAppVersion?type=1',
				params: {},
				success: function(data) {
					data = eval('(' + data + ')');
					!!fn && fn(data.paramMap);
				}
			}, "0");
		},
		init: function() {
			return;
			if(localStorage.getItem("userId") && localStorage.getItem("userId").indexOf("object") > -1) {
				localStorage.setItem("isLogined", "0");
				localStorage.setItem("userId", "");
			}
			var $token = Common.getQueryString('token');
			var $isLogin = localStorage.getItem("isLogined");
			//有身份的时候 如果地址栏带了token 和存在本地的token 不一致 则重新验证身份
			//有身份的时候 如果地址栏带了token 和存在本地的token 一致 则跳过验证
			if($isLogin == 1) {
				if(!!$token /*&& $token != localStorage.getItem("token")*/ ) {
					Common.checkToken($token);
				}
			}
			//无身份的时候 地址栏带了token 则验证
			if(!$isLogin || $isLogin == 0) {
				if(!!$token) {
					Common.checkToken($token);
				}
			}
			//微信绑定登陆
			//Common.getUserIdByOpenId();

			$(":input").blur(function() {
				$("#wechat_menu").show();
			});
			$(":input").bind("touchstart", function() {
				$("#wechat_menu").hide();
			});
			$(":input").bind("touchmove", function() {
				$("#wechat_menu").show();
			});
			$(":input").focus(function() {
				$("#wechat_menu").hide();
			});
			$(".ui-content").bind("click", function() {
				$("#child_menu").hide();
				$("#mid_menu").hide();
				Common.isOpened = false;
				Common.isOpened_mid = false;
			});
		},
		//		getUserIdByOpenId: function() {
		//			if(!!$.cookie("openid")){
		//				$.M.post({
		//				method: "/rest/broker/getUserIdByOpenId?openid=" + $.cookie("openid"),
		//				success: function(data) {
		//					data = eval('(' + data + ')');
		//					if(data.objValue) {
		//						Common.storage.setItem("userId", data.objValue);
		//						Common.storage.setItem("isLogined", "1");
		//					}
		//				},
		//				error: function(XMLHttpRequest, textStatus, errorThrown) {
		//
		//				}
		//			}, "0");
		//			}
		//			
		//		},
		//		getUserIdByOpenIdWithJump: function() {
		//			var params = {
		//				'openid': $.cookie("openid")
		//			};
		//			$.M.post({
		//				method: "/rest/broker/getUserIdByOpenId",
		//				params: params,
		//				success: function(data) {
		//					data = eval('(' + data + ')');
		//					if(data.objValue) {
		//						Common.storage.setItem("userId", data.objValue);
		//						Common.storage.setItem("isLogined", "1");
		//						setTimeout('window.location.href = "myAccount.html?noRefresh=1&from=' + Common.storage.getItem("from") + '"', 30);
		//					}
		//				},
		//				error: function(XMLHttpRequest, textStatus, errorThrown) {
		//
		//				}
		//			}, "0");
		//		},
		showMenu: function() {
			//var pageName = location.pathname.match(/\/([0-9a-zA-Z\-]*).html/)[1];
			var pageName = "1";
			//var _vheader = this.getQueryString('vheader');		
			var _vheader = Common.storage.getItem('vheader');
			if(_vheader == "null" || _vheader == null || _vheader == "") {
				_vheader = this.getQueryString('vheader');
				Common.storage.setItem('vheader', _vheader);
			}

			var level = {
				1: ["propertyList", "shareHouse"],
				2: ["newsList", "houseNewsList", "hotNewsList"],
				3: ["recommendPage", "bespeakPage"],
				4: ["registerPage", "myAccount", "myAccount2", "accountSettings", "myCouponList", "useCoupon", "previewBuilding"]
			}
			var flag = false;
			for(var i in level) {
				if(level[i].indexOf(pageName) > -1) {
					flag = i;
				}
			}
			if(_vheader == 1) {
				flag = false;
			}
			if(!!flag) {
				var arrString = [
					'<div id="footer-ment">',
					'<div class="wechat_menu" id="wechat_menu">'
				];
				if(flag == 1) {
					arrString.push('<a data-role="button" class="wechat_btn" id="house_btn" href="javascript:Common.goPage(1)"><img src="./resource/css/images/bottomtab/icon01At@2x.png"><span class="red">楼盘</span></a>');
				} else {
					arrString.push('<a data-role="button" class="wechat_btn" id="house_btn" href="javascript:Common.goPage(1)"><img src="./resource/css/images/bottomtab/ico01@2x.png">楼盘</a>');
				}
				if(flag == 2) {
					arrString.push('<a data-role="button" class="wechat_btn" id="info_btn" href="javascript:Common.goPage(8)"><img src="./resource/css/images/bottomtab/icon02At@2x.png"><span class="red">资讯</span></a>');
				} else {
					arrString.push('<a data-role="button" class="wechat_btn" id="info_btn" href="javascript:Common.goPage(8)"><img src="./resource/css/images/bottomtab/icon02@2x.png">资讯</a>');
				}
				if(flag == 3) {
					arrString.push('<a data-role="button" class="wechat_btn" id="mid_btn" href="javascript:Common.goPage(2)"><img src="./resource/css/images/bottomtab/icon03At@2x.png"><span class="red">推荐</span></a>');
				} else {
					arrString.push('<a data-role="button" class="wechat_btn" id="mid_btn" href="javascript:Common.goPage(2)"><img src="./resource/css/images/bottomtab/ico03@2x.png">推荐</a>');
				}
				if(flag == 4) {
					arrString.push('<a data-role="button" class="wechat_btn" id="my_btn" href="javascript:Common.showChild(1)"><img src="./resource/css/images/bottomtab/icon04At@2x.png"><span class="red">我的</span></a>');
				} else {
					arrString.push('<a data-role="button" class="wechat_btn" id="my_btn" href="javascript:Common.showChild(1)"><img src="./resource/css/images/bottomtab/icon04@2x.png">我的</a>');
				}
				arrString.push(['</div>',
					'<div class="child_menu" id="child_menu">',
					'<a data-role="button" class="child_btn" id="register_btn" href="javascript:Common.goPage(3)">马上注册</a>',
					'<a data-role="button" class="child_btn" id="login_btn" href="javascript:Common.goPage(4)">会员登录</a>',
					'<a data-role="button" class="child_btn" id="account_btn" href="javascript:Common.goPage(5)">我的帐号</a>',
					'<a data-role="button" class="child_btn" id="download_btn" href="javascript:Common.goPage(6)">下载APP</a>',
					'<img src="./resource/css/images/detail_wechat.png">',
					'</div>',
					'</div>'
				].join(""));
				$('[data-role="page"]').append(arrString.join(''));
				$('#footer-ment a').button();
			} else {
				$('.phone_div').css({
					bottom: "0"
				})
			}
		}
	};

	$(document).bind("pageshow", function(e) {
		var id = e.target.id;
		Common.init();
		Common.showMenu();
		var arr = ['myAccount2.html', 'myAccount.html'];
		var flag = false;
		for(var i = 0; i < arr.length; i++) {
			if(location.href.indexOf(arr[i]) > -1) {
				flag = true;
			}
		}
		if(flag == true) {
			if(!!localStorage.getItem('userId')) {
				$('body').show();
			} else {
				window.location.href = "login.html?from=" + localStorage.getItem('from');
				return;
			}
		} else {
			$('body').show();
		}
	});
	window.Common = Common;
	return Common;
});
var Initial = {
	showPop: function() {
		return;
		$("body").append('<div class="personalInfo_div" id="J_personalInfo">' +
			'<div class="select-h" onclick="javascript:;">&nbsp;</div>' +
			'<div class="select_top">' +
			'<p>请您完善“个人信息”</p>' +
			'<p>后完成邀请注册</p>' +
			'</div>' +
			'<div class="select_bottom">' +
			'<span class="confirm_btn" onclick="Initial.closePop()">点击前往</span>' +
			'</div>' +
			'</div>')
	},
	closePop: function() {
		window.location.href = "accountInfo.html"
	}
}